package com.usian.skill.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.usian.skill.common.CodeMsg;
import com.usian.skill.common.Result;
import com.usian.skill.pojo.SkillUser;
import com.usian.skill.pojo.User;
import com.usian.skill.service.GoodsService;
import com.usian.skill.service.SkillUserService;
import com.usian.skill.vo.GoodsDetailVo;
import com.usian.skill.vo.GoodsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

@Controller
@RequestMapping("/goods")
public class GoodsController {

	@Autowired
	SkillUserService userService;

	@Autowired
	GoodsService goodsService;
	
	@Autowired
	ThymeleafViewResolver thymeleafViewResolver;
	
	@Autowired
	ApplicationContext applicationContext;

	@Autowired
	StringRedisTemplate stringRedisTemplate;
	
	/**
	 * QPS:1267 load:15 mysql
	 * 5000 * 10
	 * QPS:2884, load:5 
	 * */

	/**
	 * 商品列表查询
	 * @param request
	 * @param response
	 * @param model
	 * @param user
	 * @return
	 */
    @RequestMapping(value="/to_list", produces="text/html")
    @ResponseBody
    public String list(HttpServletRequest request, HttpServletResponse response, Model model, SkillUser user) {

    	//先从redis中获取
		List<GoodsVo> goodsList = goodsService.findAll();
		model.addAttribute("goodsList",goodsList);
		//创建webContext上下文对象
		WebContext ctx = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
		//thymeleafViewResolver去渲染页面
		String s = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
		//将s保存在redis中
		stringRedisTemplate.opsForValue().set("goodsList", s,60);
		return  s;

    }

	/**
	 * 商品的详情
	 * @param request
	 * @param response
	 * @param model
	 * @param user
	 * @param goodsId
	 * @param session
	 * @return
	 */

    @RequestMapping(value="/detail/{goodsId}")
    @ResponseBody
    public Result<GoodsDetailVo> detail(HttpServletRequest request, HttpServletResponse response, Model model, SkillUser user,
						 @PathVariable("goodsId")long goodsId, HttpSession session) {

		String userId = session.getAttribute("userId").toString();
		if (userId==null){
			return Result.error(CodeMsg.SERVER_ERROR);
		}
		//从redis中获取数据
		GoodsVo	goods = goodsService.goodsVoById(goodsId);

		SkillUser skillUser = new SkillUser();
		skillUser.setId(Long.parseLong(userId));
		//将实际转换成毫秒值
		long startAt = goods.getStartDate().getTime();
		long endAt = goods.getEndDate().getTime();
		//获取系统的当前时间
		long now = System.currentTimeMillis();
		int miaoshaStatus = 0;
		int remainSeconds = 0;
		if(now < startAt ) {//秒杀还没开始，倒计时
			miaoshaStatus = 0;
			remainSeconds = (int)((startAt - now )/1000);
		}else  if(now > endAt){//秒杀已经结束
			miaoshaStatus = 2;
			remainSeconds = -1;
		}else {//秒杀进行中
			miaoshaStatus = 1;
			remainSeconds = 0;
		}
		GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
		goodsDetailVo.setUser(skillUser);
		goodsDetailVo.setRemainSeconds(remainSeconds);
		goodsDetailVo.setMiaoshaStatus(miaoshaStatus);
		goodsDetailVo.setGoods(goods);
		return Result.success(goodsDetailVo);
    }



	//测试redis的session
	@RequestMapping(value="/to_detail2/{goodsId}",produces="text/html")
	@ResponseBody
	public String detail2(HttpServletRequest request, HttpServletResponse response, Model model,SkillUser user,
						  @PathVariable("goodsId")long goodsId,HttpSession session) {
		String userId = session.getAttribute("userId").toString();
		return session.getId()+"--------------"+userId;
	}


}
